package com.healthcloud.controller;

import java.util.Arrays;
import java.util.List;

import com.healthcloud.annotation.SysLog;
import com.healthcloud.entity.*;
import com.healthcloud.service.TestprojectkindService;
import com.healthcloud.utils.ApiRes;
import com.healthcloud.utils.PageParam;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.healthcloud.service.TestprojectitemService;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 检测项目（子项）
 *
 * @author yucl
 * @email yucailong1111@126.com
 * @date 2017-11-16 13:53:41
 */
@ApiIgnore
@Controller
@RequestMapping("testprojectitem")
public class TestprojectitemController {
	@Autowired
	private TestprojectitemService testprojectitemService;

	@Autowired
    private TestprojectkindService testprojectkindService;
	/**
	 * 列表
	 */
    @RequestMapping(value="/toTestprojectitem",method = RequestMethod.GET)
    public String totestreferencevalue(){
        return "bus/testprojectitem/testprojectitemmgr";
    }

    /**
     * 显示
     */
	@RequestMapping(value = "/searchTestprojectitem")
	@ResponseBody
    public MsgVo  searchTestprojectitem(TestprojectitemEntity bean,Model model, @ModelAttribute("pageParam")PageParam pageParam){
        List<TestprojectitemEntity> list = testprojectitemService.queryList(bean, pageParam);
        int count = testprojectitemService.queryTotal(bean);
        if(list != null) {
            for(TestprojectitemEntity testprojectitemEntity : list) {
                if(testprojectitemEntity.getIsDel() == 0) {
                    testprojectitemEntity.setDel("否");
                }else {
                    testprojectitemEntity.setDel("是");
                }
            }
        }
        return new MsgVo(list,count,count,pageParam.getsEcho());
    }

	/**
	 * 新增页
	 */
	@RequestMapping("/tosave")
	@RequiresPermissions("testprojectitem:save")
    public String toSave(Model model) {
        return "bus/testprojectitem/addOrUpdatetestprojectitem";
    }

	/**
	 * 保存
	 */
    @SysLog("保存Testprojectitem")
    @RequestMapping("/save")
    @ResponseBody
    public ApiRes save(TestprojectitemEntity entity, String testKindCode) {
        entity.setTestKindCode(testKindCode);
        testprojectitemService.save(entity);
        TestprojectkindEntity testprojectkindEntity = testprojectkindService.queryObject(testKindCode);
        entity.setTestKindName(testprojectkindEntity.getTestKindName());
        return ApiRes.SUCCESS;
    }

	/**
	 * 修改页
	 */
	@RequestMapping("/toupdate")
    @RequiresPermissions("testprojectitem:update")
    public String toUpdate(@Param("testItemCode") String testItemCode, Model model) {

        TestprojectitemEntity entity = testprojectitemService.queryObject(testItemCode);
        model.addAttribute("entity", entity);

        TestprojectkindEntity testprojectkindEntity = testprojectkindService.queryObject(entity.getTestKindCode());
        model.addAttribute("testprojectkind",testprojectkindEntity);
        return "bus/testprojectitem/addOrUpdatetestprojectitem";
    }

    /**
     * 修改
     */
    @SysLog("修改Testprojectitem")
    @RequestMapping("/update")
    @ResponseBody
    public ApiRes update(TestprojectitemEntity entity, String testKindCode) {

        entity.setTestKindCode(testKindCode);
        testprojectitemService.update(entity);
        TestprojectkindEntity testprojectkindEntity = testprojectkindService.queryObject(testKindCode);
        entity.setTestKindName(testprojectkindEntity.getTestKindName());
        return ApiRes.SUCCESS;
    }


    /**
     * 删除
     */
    @SysLog("删除Testprojectitem")
    @RequestMapping("/delete")
    @RequiresPermissions("testprojectitem:delete")
    @ResponseBody
    public ApiRes delete(String[] testItemCodes) {
        testprojectitemService.deleteBatch(testItemCodes);
        return ApiRes.SUCCESS;
    }

    /**
     * 查询列表
     */
    @RequestMapping("/select")
    @ResponseBody
    public ApiRes selectTestProjectItem(TestprojectitemEntity bean, @ModelAttribute("pageParam")PageParam pageParam){
        List<TestprojectitemEntity> list = testprojectitemService.queryList(bean, pageParam);
        for(TestprojectitemEntity testprojectitemEntity : list) {
            testprojectitemEntity.setId(testprojectitemEntity.getTestItemCode());
        }
        int count = testprojectitemService.queryTotal(bean);
        return new ApiRes(true,list,null,String.valueOf(count));
    }

    /**
     * 根据子项目编号查询
     */
    @RequestMapping(value = "/queryByItemCode", method = RequestMethod.GET)
    @ResponseBody
    public ApiRes queryByItemCode (String testItemCode) {
        if(testItemCode != null && !"".equals(testItemCode)) {
            TestprojectitemEntity entity = testprojectitemService.queryByItemCode(testItemCode);
            return new ApiRes(true,entity,"","");
        }
        return new ApiRes(false,null,"testItemCode不能为空","testItemCode不能为空");
    }

    /**
     * 根据多个子项目编号查询
     */
    @RequestMapping(value = "/queryByItemCodes", method = RequestMethod.GET)
    @ResponseBody
    public ApiRes queryByItemCodes (String[] testItemCodes) {
        if(testItemCodes != null && !"".equals(testItemCodes)) {
            List<String> testlist = Arrays.asList(testItemCodes);
            List<TestprojectitemEntity> list = testprojectitemService.queryByItemCodes(testlist);
            return new ApiRes(true,list,"","");
        }
        return new ApiRes(false,null,"testItemCode不能为空","testItemCode不能为空");
    }
}
